package com.bosicloud.cmb.ibp.aws.adapter.controller;

import com.bosicloud.cmb.ibp.aws.adapter.commom.constant.UrlDefine;
import com.bosicloud.cmb.ibp.aws.adapter.commom.dto.CreateApiParamDTO;
import com.bosicloud.cmb.ibp.aws.adapter.commom.dto.CreateApiResultDTO;
import com.bosicloud.cmb.ibp.aws.adapter.commom.dto.DeployApiParamDTO;
import com.bosicloud.cmb.ibp.aws.adapter.commom.dto.DeployApiResultDTO;
import com.bosicloud.cmb.ibp.aws.adapter.commom.vo.ApiKeyVO;
import com.bosicloud.cmb.ibp.aws.adapter.commom.vo.CreateApiKeyVO;
import com.bosicloud.cmb.ibp.aws.adapter.service.ApiGatewayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;

/**
 * @author Jack Peng / ziliang.peng@bosicloud.com
 */
@Slf4j
@RestController
@Api(value = "API Gateway接口", description = "AWS API Gateway")
public class ApiGatewayController {

    @Autowired
    ApiGatewayService gatewayService;

    @ApiOperation(value = "创建API Key")
    @PostMapping(value = UrlDefine.GW_CREATE_API_KEY)
    public ApiKeyVO createApiKey(@RequestBody @Valid CreateApiKeyVO apiKeyVO) {

        return gatewayService.createApiKey(apiKeyVO.getUserId(), apiKeyVO.getIsTest());

    }

    @ApiOperation(value = "创建API Gateway")
    @PostMapping(value = UrlDefine.GW_CREATE_API)
    public CreateApiResultDTO createApi(@RequestBody @Valid CreateApiParamDTO createApiParamDTO) throws IOException {

        return gatewayService.createApiGateway(createApiParamDTO.getJsonUrl(), false);
    }

    @ApiOperation(value = "部署API Gateway")
    @PostMapping(value = UrlDefine.GW_DEPLOY_API)
    public DeployApiResultDTO deployApi(@RequestBody @Valid DeployApiParamDTO deployApi) throws IOException {

        return gatewayService.deployApi(deployApi);

    }

    @ApiOperation(value = "删除API Gateway")
    @PostMapping(value = UrlDefine.GW_DELETE_API)
    public boolean deployApi(@RequestParam String awsApiId) throws IOException {

        return gatewayService.deleteApiGateway(awsApiId);

    }

}
